home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / artdisplay / LocalCoverArtSearch.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  6.6 KB  |  185 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*- 
  2. #
  3. # Copyright (C) 2006 - Ed Catmur <ed@catmur.co.uk>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  11. # GStreamer plugins to be used and distributed together with GStreamer
  12. # and Rhythmbox. This permission is above and beyond the permissions granted
  13. # by the GPL license by which Rhythmbox is covered. If you modify this code
  14. # you may extend this exception to your version of the code, but you are not
  15. # obligated to do so. If you do not wish to do so, delete this exception
  16. # statement from your version.
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program; if not, write to the Free Software
  24. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  25.  
  26. import os
  27. import rhythmdb
  28. import gnomevfs
  29. import rb
  30. import gobject
  31.  
  32. IMAGE_NAMES = ["cover", "album", "albumart", ".folder", "folder"]
  33. LOAD_DIRECTORY_FLAGS = gnomevfs.FILE_INFO_GET_MIME_TYPE | gnomevfs.FILE_INFO_FORCE_FAST_MIME_TYPE | gnomevfs.FILE_INFO_FOLLOW_LINKS
  34. ITEMS_PER_NOTIFICATION = 10
  35. ART_SAVE_NAME = 'Cover.jpg'
  36. ART_SAVE_FORMAT = 'jpeg'
  37. ART_SAVE_SETTINGS = {"quality": "100"}
  38.  
  39. def file_root (f_name):
  40.     return os.path.splitext (f_name)[0].lower ()
  41.  
  42. def shared_prefix_length (a, b):
  43.     l = 0
  44.     while a[l] == b[l]:
  45.         l = l+1
  46.     return l
  47.  
  48.  
  49. class LocalCoverArtSearch:
  50.     def __init__ (self):
  51.         pass
  52.  
  53.     def _load_dir_cb (self, handle, files, exception, (results, on_search_completed_cb, entry, args)):
  54.         for f in files:
  55.             if f.mime_type is None:
  56.                 pass
  57.             elif f.mime_type.startswith ("image/") and f.permissions & gnomevfs.PERM_USER_READ:
  58.                 results.append (f.name)
  59.         if exception:
  60.             on_search_completed_cb (self, entry, results, *args)
  61.             if not issubclass (exception, gnomevfs.EOFError):
  62.                 print "Error reading \"%s\": %s" % (self.uri.parent, exception)
  63.  
  64.     def search (self, db, entry, on_search_completed_cb, *args):
  65.         self.uri = None
  66.         try:
  67.             self.uri = gnomevfs.URI (entry.get_playback_uri())
  68.         except TypeError:
  69.             pass
  70.  
  71.         if self.uri is None or self.uri.scheme == 'http':
  72.             print 'not searching for local art for %s' % (self.uri)
  73.             on_search_completed_cb (self, entry, [], *args)
  74.             return
  75.  
  76.         self.artist = db.entry_get (entry, rhythmdb.PROP_ARTIST)
  77.         self.album = db.entry_get (entry, rhythmdb.PROP_ALBUM)
  78.  
  79.         print 'searching for local art for %s' % (self.uri)
  80.         gnomevfs.async.load_directory (self.uri.parent, self._load_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data=([], on_search_completed_cb, entry, args))
  81.  
  82.     def search_next (self):
  83.         return False
  84.  
  85.     def get_best_match_urls (self, results):
  86.         # Compare lower case, without file extension
  87.         for name in [file_root (self.uri.short_name)] + IMAGE_NAMES:
  88.             for f_name in results:
  89.                 if file_root (f_name) == name:
  90.                     yield self.uri.parent.append_file_name (f_name)
  91.  
  92.         # look for file names containing the artist and album (case-insensitive)
  93.         # (mostly for jamendo downloads)
  94.         artist = self.artist.lower()
  95.         album = self.album.lower()
  96.         for f_name in results:
  97.             f_root = file_root (f_name).lower()
  98.             if f_root.find (artist) != -1 and f_root.find (album) != -1:
  99.                 yield self.uri.parent.append_file_name (f_name).path
  100.  
  101.         # if that didn't work, look for the longest shared prefix
  102.         # only accept matches longer than 2 to avoid weird false positives
  103.         match = (2, None)
  104.         for f_name in results:
  105.             pl = shared_prefix_length(f_name, self.uri.short_name)
  106.             if pl > match[0]:
  107.                 match = (pl, f_name)
  108.  
  109.         if match[1] is not None:
  110.             yield self.uri.parent.append_file_name (match[1]).path
  111.  
  112.     def pixbuf_save (self, plexer, pixbuf, uri):
  113.         gnomevfs.async.create (uri, plexer.send (), gnomevfs.OPEN_WRITE | gnomevfs.OPEN_TRUNCATE, False, 0644, gnomevfs.PRIORITY_DEFAULT)
  114.         yield None
  115.         _, (handle, result) = plexer.receive ()
  116.         if result:
  117.             print "Error creating \"%s\": %s" % (uri, result)
  118.             return
  119.         def pixbuf_cb (buf):
  120.             data = [buf]
  121.             status = []
  122.             def write_coro (w_plexer):
  123.                 while data:
  124.                     buf = data.pop ()
  125.                     handle.write (buf, w_plexer.send ())
  126.                     yield None
  127.                     _, (_, requested, result, written) = w_plexer.receive ()
  128.                     if result:
  129.                         print "Error writing \"%s\": %s" % (uri, result)
  130.                         status.insert (0, False)
  131.                         return
  132.                     if written < requested:
  133.                         data.insert (0, buf[written:])
  134.                 status.insert (0, True)
  135.             rb.Coroutine (write_coro).begin ()
  136.             while not status:
  137.                 gobject.main_context_default ().iteration ()
  138.             return status[0]
  139.         pixbuf.save_to_callback (pixbuf_cb, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
  140.         handle.close (plexer.send())
  141.         yield None
  142.         _, (_, result) = plexer.receive ()
  143.         if result:
  144.             print "Error closing \"%s\": %s" % (uri, result)
  145.  
  146.     def _save_dir_cb (self, handle, files, exception, (db, entry, pixbuf)):
  147.         artist, album = [db.entry_get (entry, x) for x in [rhythmdb.PROP_ARTIST, rhythmdb.PROP_ALBUM]]
  148.         for f in files:
  149.             if f.mime_type.split ("/")[0] in ["image", "x-directory"]:
  150.                 continue
  151.             uri = str (self.uri.parent.append_file_name (f.name))
  152.             u_entry = db.entry_lookup_by_location (uri)
  153.             if u_entry:
  154.                 u_artist, u_album = [db.entry_get (u_entry, x) for x in [rhythmdb.PROP_ARTIST, rhythmdb.PROP_ALBUM]]
  155.                 if album != u_album:
  156.                     print "Not saving local art; encountered media with different album (%s, %s, %s)" % (uri, u_artist, u_album)
  157.                     handle.cancel ()
  158.                     return
  159.                 continue
  160.             print "Not saving local art; encountered unknown file (%s)" % uri
  161.             handle.cancel ()
  162.             return
  163.         if exception:
  164.             if issubclass (exception, gnomevfs.EOFError):
  165.                 art_uri = str (self.uri.parent.append_file_name (ART_SAVE_NAME))
  166.                 print "Saving local art to \"%s\"" % art_uri
  167.                 rb.Coroutine (self.pixbuf_save, pixbuf, art_uri).begin ()
  168.             else:
  169.                 print "Error reading \"%s\": %s" % (self.uri.parent, exception)
  170.  
  171.     def save_pixbuf (self, db, entry, pixbuf):
  172.         self.uri = None
  173.         try:
  174.             self.uri = gnomevfs.URI (entry.get_playback_uri())
  175.         except TypeError:
  176.             pass
  177.  
  178.         if self.uri is None or self.uri.scheme == 'http':
  179.             print 'not saving local art for %s' % self.uri
  180.             return
  181.  
  182.         print 'checking whether to save local art for %s' % self.uri
  183.         gnomevfs.async.load_directory (self.uri.parent, self._save_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data=(db, entry, pixbuf))
  184.